package com.fl.niu.until;

import com.fl.niu.bo.AdminUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @Author Aijm
 * @Description 根据SecurityContext 获取信息
 * @Date 2020/3/26
 */
@Slf4j
public class SecurityUtil {



    /**
     * 获取SecurityUser
     * @return
     */
    public static AdminUserDetails getSecurityUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        return (AdminUserDetails)authentication.getPrincipal();
    }


    /**
     * 获取登录用户的信息
     * @return
     */
    public static Long getUserId(){
        try {
            return getSecurityUser().getUmsAdmin().getId();
        } catch (Exception e) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
    }

}
